package com.hotelvip.common.aop;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hotelvip.common.annotation.VipUserAnno;
import com.hotelvip.common.pojo.VipCodeFirst;
import com.hotelvip.dao.UserTurnVipMapper;
import com.hotelvip.dao.VipUserLogMpper;
import com.hotelvip.dao.VipUserMapper;
import com.hotelvip.dto.VipUserDTO;
import com.hotelvip.entity.UserTurnVip;
import com.hotelvip.entity.VipUser;
import com.hotelvip.entity.VipUserLog;
import com.hotelvip.enumEntity.CardStatueEnum;
import com.hotelvip.service.batch.VipUserLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 写入会员升级日志
 * @author Lzh
 * @date 2021/1/19 8:40
 */
@Component
@Aspect
@Slf4j
@Transactional(rollbackFor = RuntimeException.class)
public class VipUserAop {

    @Autowired
    private VipUserMapper vipUserMapper;
    @Autowired
    VipCodeFirst vipCodeFirst;
    @Autowired
    UserTurnVipMapper userTurnVipMapper;
    @Autowired
    VipUserLogMpper vipUserLogMpper;
    @Autowired
    VipUserLogService vipUserLogService;


    /**
     * 获取参数和返回值
     * @param joinPoint
     * @param vipUserAnno
     * @return
     */
    @Around("@annotation(vipUserAnno)")
    public void around(ProceedingJoinPoint joinPoint, VipUserAnno vipUserAnno){
        //获取注解内容
        String key = vipUserAnno.key();
        //获取参数
        Object[] args = joinPoint.getArgs();
        Object result = null;
        //写入用户升级会员的日志
        if (key.equals("createVip")){
            log.info("添加会员升级日志");
            VipUserDTO vipUserDTO = (VipUserDTO) args[0];
            try {
                result = joinPoint.proceed();
                Long vipUserId = Long.valueOf(result.toString());
                VipUser vipUser = vipUserMapper.selectById(vipUserId);
                Long vipCode = saveVipCode(vipUser);
                saveVipUserLog(vipUser,vipUserDTO,vipCode);

            } catch (Throwable throwable) {
                throwable.printStackTrace();
                throw new RuntimeException(throwable);
            }

        }

        if (key.equals("updateStatus")){
            log.info("修改会员状态");
            try {
                result = joinPoint.proceed();
                List<VipUser> vipUsers = (List<VipUser>) result;
                for (VipUser vipUser : vipUsers) {
                    if (vipUser.getStatus()==4) {
                        VipUserLog vipUserLog = new VipUserLog();
                        vipUserLog.setEvent(CardStatueEnum.four.getName());
                        vipUserLogMpper.update(vipUserLog, new UpdateWrapper<VipUserLog>().eq("vip_code", vipUser.getVipCode()));
                    }
                }



            } catch (Throwable throwable) {
                throwable.printStackTrace();
                throw new RuntimeException(throwable);
            }
        }


    }

    private void saveVipUserLog(VipUser vipUser, VipUserDTO vipUserDTO, Long vipCode) {
        VipUserLog vipUserLog = new VipUserLog();
        vipUserLog.setUserId(vipUser.getUserId())
                .setEvent("用户升级会员")
                .setName(vipUserDTO.getUser().getName())
                .setVipCode(vipCode)
                .setVipName(vipUserDTO.getVipName())
                .setIssuer(vipUserDTO.getVipCardVo().getVipCard().getIssuer())
                .setPub(vipUserDTO.getVipCardVo().getVipCard().getPub())
                .setVipType(vipUserDTO.getVipCardVo().getVipCard().getVipType())
                .setUpload(vipUser.getStartDate())
                .setDueTime(vipUser.getEndDate())
                .setCardId(vipUserDTO.getVipCardVo().getVipCard().getId())
                .setComment(vipUser.getComment());
        vipUserLogMpper.insert(vipUserLog);
    }

    private Long saveVipCode(VipUser vipUser){
        //获取酒店号码前缀并拼接id判断位数，规定位数大于8位长度
        String[] vipFists = vipCodeFirst.getVipFist().split(",");
        //todo 店号字段 vipFirst
        String vipFist = vipFists[1];
        StringBuilder tempVipCode = new StringBuilder(vipFist).append(vipUser.getId());
        while (tempVipCode.length()<8){
            tempVipCode.insert(vipFist.length(), "0");
        }
        vipUser.setVipCode(Long.parseLong(tempVipCode.toString()));
        vipUserMapper.updateById(vipUser);
        UserTurnVip userTurnVip = new UserTurnVip();
        userTurnVip.setUserId(vipUser.getUserId()).setVipCode(Long.parseLong(tempVipCode.toString()));
        userTurnVipMapper.insert(userTurnVip);
        return Long.parseLong(tempVipCode.toString());


    }

}
